package cn.gov.msa.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.github.pagehelper.PageHelper;

import cn.gov.msa.dao.ConsultingMapper;
import cn.gov.msa.model.Consulting;
import cn.gov.msa.pagehelper.CustomPageHelper;
import cn.gov.msa.service.ConsultingService;

public class ConsultingServiceImpl implements ConsultingService{

	@Autowired
	private ConsultingMapper consultingMapper;
	
	@Override
	public List<Consulting> selectRepository(int pageNum, int pageSize, Consulting consulting) throws Exception {
		CustomPageHelper.startPage(pageNum, pageSize); // 核心分页代码  
		List<Consulting> listConsultings = consultingMapper.selectData(consulting);
		
		return listConsultings;
	}

	@Override
	public boolean saveData(Consulting consulting) throws Exception {
		boolean result = false;
		
		//留言时间为当前系统时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date nowDate = new Date();
		String leaveTime = sdf.format(nowDate);
		consulting.setLeaveTime(leaveTime);
		
		//状态默认未处理
		consulting.setComplainStatus("0");
		int iconsulting = consultingMapper.insert(consulting);
		if(iconsulting > 0)
			result = true;
		
		return result;
	}

	@Override
	public boolean updateData(Consulting consulting) throws Exception {
		boolean result = false;
		
		//状态改为已处理
		consulting.setComplainStatus("1");
		
		//回复日期为当前系统时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date nowDate = new Date();
		String replyTime = sdf.format(nowDate);
		consulting.setReplyTime(replyTime);
		
		int iconsulting = consultingMapper.updateByPK(consulting);
		if(iconsulting > 0)
			result = true;
		
		return result;
	}

	
}
